import request from './request.js'
import $store from '@/store/index.js'
export default {
	_getUrlParams(search) {
		search = search || location.search;
		var paramsSplit = search.replace(/^[^\?]*\?/i, '').split('&');
		var params = {};
		if (Array.isArray(paramsSplit)) {
			paramsSplit.forEach(function(item) {
				const itemSplit = item.split('=');
				params[itemSplit[0]] = itemSplit[1];
			});
		}
		return params
	},
	// 微信公众号授权
	wxAuthorize() {

		let link = window.location.href;
		let params = this._getUrlParams(link); // 地址解析
		// uni.clearStorageSync();
		if (params.invite_code) {
			uni.setStorageSync('keyInvite_code', params.invite_code)
		}
		if (params.agent_code_id) {
			uni.setStorageSync('keyAgent_code_id', params.agent_code_id)
		}
		if (params.store_id) {
			uni.setStorageSync('keyStore_id', params.store_id)
		}
		// 已经授权登录过的就不用再授权了
		if (uni.getStorageSync('keyOpenid')) return;
		// 如果拿到code，调用授权接口，没有拿到就跳转微信授权链接获取
		if (params.code) {
			this.backCode();
		} else {
			let appid = 'wx73b2fe7a7e0dab2c';
			let uri = encodeURIComponent(link);
			//uri='https%3A%2F%2Fkf.hbciya.com%2Fh5%2Findex.html%23%2F';
			let authURL =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
			window.location.href = authURL;
			this.backCode();
		}
	},
	// 获取授权回调里的code
	backCode() {
		let rlink = window.location.href;
		let dataparams = this._getUrlParams(rlink);
		uni.request({
			url: request.common.baseUrl + '/api/access/userOpenid', //仅为示例，并非真实接口地址。
			data: {
				code: dataparams.code
			},
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success: (res) => {
				if (res.data.code == 200) {
					uni.setStorageSync('keyOpenid', res.data.data.openid)
					$store.state.user.openid = res.data.data.openid
					uni.request({
						url: request.common.baseUrl + '/api/access/userinfo', //仅为示例，并非真实接口地址。
						data: {
							access_token: res.data.data.access_token,
							openid: res.data.data.openid
						},
						header: {
							'Content-Type': 'application/json;charset=UTF-8',
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						success: (userinfores) => {
							if (userinfores.data.code == 200) {
								uni.setStorageSync('keyNickname', userinfores.data.data.nickname)
								uni.setStorageSync('keyDistributor_state', userinfores.data.data.distributor_state)
								uni.setStorageSync('keyHeadimgurl', userinfores.data.data.headimgurl)
								uni.setStorageSync('keySex', userinfores.data.data.sex)
								uni.setStorageSync('keywCity', userinfores.data.data.city)
								$store.state.user.nickname = userinfores.data.data.nickname,
									$store.state.user.sex = userinfores.data.data.sex
							}
						}
					});
				}
			}
		});
	},
	// 更新tabbar角标
	updateCartBadge(count) {
		if (count > 0) {
			return uni.setTabBarBadge({
				index: 2,
				text: count.toString()
			})
		}
		uni.removeTabBarBadge({
			index: 2
		})
	},
	// 判断订单状态
	formatStatus(order) {
		if (!order) {
			return ''
		}
		// 未支付
		if (!order.paid_time) {
			return "待支付"
		}
		// 退款情况
		if (order.refund_status !== 'pending') {
			switch (order.refund_status) {
				case 'applied':
					return '退款中'
					break;
				case 'success':
					return '退款成功'
					break;
				case 'failed':
					return '退款失败'
					break;
			}
		}
		switch (order.ship_status) {
			case 'pending':
				return '待发货'
				break;
			case 'delivered':
				return '待收货'
				break;
			case 'received':
				return '已签收'
				break;
		}
	},
}
